/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.hibernate.util;

import com.wisnu.hibernate.service.PtikDao;
import com.wisnu.hibernate.service.impl.PtikDaoImpl;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Wisnu Wardoyo
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static PtikDao ptikDao;

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                sessionFactory = new Configuration()
                        .configure("com/wisnu/hibernate/resources/hibernate.cfg.xml")
                        .buildSessionFactory();
                Configuration configuration = new Configuration();

            } catch (HibernateException ex) {
                // Log the exception. 
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
        return sessionFactory;
    }

    public static PtikDao getPtikDao() {
        if (ptikDao == null) {
            ptikDao = new PtikDaoImpl(getSessionFactory());
        }
        return ptikDao;
    }

}
